package com.hornsun.data.repository;

import com.hornsun.data.constant.types.Whether;
import com.hornsun.data.dbo.Organization;
import com.hornsun.data.dbo.OrganizationExample;
import com.hornsun.data.repository.mapper.OrganizationMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by 编译中... on 2018/7/11.
 */
@Repository
public class OrganizationRepository {

    @Autowired
    private OrganizationMapper organizationMapper;


    public Organization getById(int id){
        return organizationMapper.selectByPrimaryKey(id);
    }

    public List<Organization> getByPid(int pid){
        OrganizationExample organizationExample = new OrganizationExample();
        organizationExample.setOrderByClause("indexs");
        OrganizationExample.Criteria criteria = organizationExample.createCriteria();
        criteria.andPidEqualTo(pid);
        return organizationMapper.selectByExample(organizationExample);
    }

    /**
     * 获取父节点下除了当前节点还存在的节点数
     * @param pid
     * @param id
     * @return
     */
    public long getCountByPidNotId(int pid,int id){
        OrganizationExample organizationExample = new OrganizationExample();
        OrganizationExample.Criteria criteria = organizationExample.createCriteria();
        criteria.andPidEqualTo(pid).andIdNotEqualTo(id);
        return organizationMapper.countByExample(organizationExample);
    }


    public List<Organization> getAll(){
        return organizationMapper.selectByExample(new OrganizationExample());
    }

    public long getCountByPId(int pid){
        OrganizationExample organizationExample = new OrganizationExample();
        OrganizationExample.Criteria criteria = organizationExample.createCriteria();
        criteria.andPidEqualTo(pid);
        return organizationMapper.countByExample(organizationExample);
    }

    /**
     * 获取最大id的值
     * @return
     */
    public int getMaxId(){
        return organizationMapper.selectMaxId();
    }

    /**
     * 通过pid获取最大的index
     * @param pid
     * @return
     */
    public int getMaxIndexByPid(int pid){
        if (getCountByPId(pid)==0){
            return 0;
        }
        return (int)organizationMapper.selectMaxIndexs(pid);
    }


    /**
     * 添加子节点
     * @param organization
     */
    public void add(Organization organization){
        if (organizationMapper.insert(organization)!=1){
            throw new IllegalArgumentException("insert organization failed by organization:"+organization);
        }
    }

    public void deleteById(int id){
        if (organizationMapper.deleteByPrimaryKey(id)!=1){
            throw new IllegalArgumentException("delete organization failed by id:"+id);
        }
    }

    public void updateById(Organization organization){
        if (organizationMapper.updateByPrimaryKeySelective(organization)!=1){
            throw new IllegalArgumentException("update organization failed by organization:"+organization);
        }
    }

    public void hadChild(int id){
        Organization organization = new Organization();
        organization.setId(id);
        organization.setHasChild((byte) Whether.YES.getType().intValue());
        organizationMapper.updateByPrimaryKeySelective(organization);
    }
}
